package com.easpeed.elevator.base;

import android.annotation.TargetApi;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import com.bigyu.utils.Logger;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;

import androidx.annotation.RequiresApi;
import androidx.fragment.app.FragmentActivity;

import com.easpeed.comon.bean.ChannelSettingBean;
import com.easpeed.comon.bean.UserOnlineStatus;
import com.easpeed.comon.utils.SocketHelper;
import com.easpeed.device.utils.UserConnectUiHelper;
import com.easpeed.elevator.event.SendOrderEvent;
import com.gyf.immersionbar.BarHide;
import com.gyf.immersionbar.ImmersionBar;

import org.greenrobot.eventbus.Subscribe;

/**
 * Created by Taylor on 2016/4/29.
 */
public abstract class BaseActivity extends FragmentActivity {

    protected SharedPreferences sp;
    protected SharedPreferences.Editor editor;

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        sp = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
        editor = sp.edit();


        Window window = getWindow();
        window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        window.setStatusBarColor(Color.TRANSPARENT);
        getIntentData(savedInstanceState);
        loadXml();
        initView();
        initData();
        registerListener();
    }

    @Override
    protected void onResume() {
        super.onResume();
        showTips(SocketHelper.isUserOnline());
    }

    @TargetApi(19)
    private void setTranslucentStatus(boolean on) {
        Window win = getWindow();
        WindowManager.LayoutParams winParams = win.getAttributes();
        final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
        if (on) {
            winParams.flags |= bits;
        } else {
            winParams.flags &= ~bits;
        }
        win.setAttributes(winParams);
    }

    @Subscribe
    public void onEventMainThread(UserOnlineStatus event) {
        Logger.e("TAG", "onEventMainThread: " + event);
        showTips(event.getOnline());
    }


    private void showTips(boolean show) {
        if (show && SocketHelper.isConnected()) {
            UserConnectUiHelper.addTips(this);
        } else {
            UserConnectUiHelper.removeTips(this);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    /**
     * 得到上一个Activity传来的Intent数据
     *
     * @param savedInstanceState
     */
    protected abstract void getIntentData(Bundle savedInstanceState);

    /**
     * 加载布局
     */
    protected abstract void loadXml();

    /**
     * 初始化控件
     */
    protected abstract void initView();

    /**
     * 初始化数据
     */
    protected abstract void initData();

    /**
     * 设置监听
     */
    protected abstract void registerListener();


    /**
     * Toast长显示
     *
     * @param msg
     */
    protected void showLongToast(String msg) {
        Toast.makeText(MyApplication.context, msg, Toast.LENGTH_LONG).show();
    }


    /**
     * Toast短显示
     *
     * @param msg
     */
    protected void showShortToast(String msg) {
        Toast.makeText(MyApplication.context, msg, Toast.LENGTH_SHORT).show();
    }


}
